# Name: JoinCanaGrid.py - MCBS 10/07/2014
# Description: Joins information on Canasat maps to grid and saves Tables/G1kmMiRCS'x'.dbf for 'x' from 2004 to 2013 and
#              Tables/G1kmMiRSP'x'.dbf for 'x' from 2003 to 2013. It calls a 'CanasatData.gdb' that should be loaded with
#              CANASAT project shapefiles.

# import system module
import arcpy
from arcpy import env
#from arcpy.sa import *


# Set workspace environment:
#env.workspace = "CanasatData.gdb"
env.workspace = "C:\Users\Marcelo\Documents\Sugarcane\Offline Data\MunicBasedData\CanasatData.gdb"
env.overwriteOutput = True
#arcpy.CheckOutExtension("Spatial")

# Folder to store tables:
PathForTables = "C:\Users\Marcelo\Documents\Sugarcane\Offline Data\MunicBasedData\Tables"

# Input grid:
GridName      = "G1kmMiR"
# Output grids name:
OutName       = "G1kmMiR"

#Years = range(2003,2013)
# Years = [2003,2004]
# Years = [2003]

# First do state of SP:

arcpy.MakeFeatureLayer_management(GridName, "GridSP")
arcpy.SelectLayerByLocation_management("GridSP", "INTERSECT", "EstadoSP")

Years = range(2003,2014)

for year in Years:
	JoinClass       = "Canasat_SP_" + str(year) 
	OutputFeature   = OutName  + "SP" + str(year)
	fieldmappings   = arcpy.FieldMappings()
	fieldmappings.addTable(GridName)
	fieldmappings.addTable(JoinClass)
#	x = fieldmappings.findFieldMapIndex("FID_Filter")
#	fieldmappings.removeFieldMap(x)
#	x = fieldmappings.findFieldMapIndex("FID_EstadoSP")
#	fieldmappings.removeFieldMap(x)
#	x = fieldmappings.findFieldMapIndex("FID_Buffer40km")
#	fieldmappings.removeFieldMap(x)
	arcpy.SpatialJoin_analysis("GridSP", JoinClass, OutputFeature,
	                           "JOIN_ONE_TO_MANY", "KEEP_ALL", fieldmappings,
							   "COMPLETELY_WITHIN", "", "")
	print "Joining attributes from " + JoinClass + " to grid."
	print "New Grid file containing attributes created: " + OutputFeature
	print " "
	# Save .dbf tables in specified folder
	TableName    = OutputFeature + ".dbf"
	arcpy.TableToTable_conversion(OutputFeature, PathForTables, TableName)
	
	
# Now do CS:

arcpy.SelectLayerByAttribute_management("GridSP", "SWITCH_SELECTION")

Years = range(2004,2014)

for year in Years:
	JoinClass       = "Canasat_CS_" + str(year) 
	OutputFeature   = OutName  + "CS" + str(year)
	fieldmappings   = arcpy.FieldMappings()
	fieldmappings.addTable(GridName)
	fieldmappings.addTable(JoinClass)
#	x = fieldmappings.findFieldMapIndex("FID_Filter")
#	fieldmappings.removeFieldMap(x)
#	x = fieldmappings.findFieldMapIndex("FID_EstadoSP")
#	fieldmappings.removeFieldMap(x)
#	x = fieldmappings.findFieldMapIndex("FID_Buffer40km")
#	fieldmappings.removeFieldMap(x)
	arcpy.SpatialJoin_analysis("GridSP", JoinClass, OutputFeature,
	                           "JOIN_ONE_TO_MANY", "KEEP_ALL", fieldmappings,
							   "COMPLETELY_WITHIN", "", "")
	print "Joining attributes from " + JoinClass + " to grid."
	print "New Grid file containing attributes created: " + OutputFeature
	print " "
	# Save .dbf tables in specified folder
	TableName    = OutputFeature + ".dbf"
	arcpy.TableToTable_conversion(OutputFeature, PathForTables, TableName)

